package service;

import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import dao.ContractModelMapper;
import domain.ContractModel;
import domain.ContractModelExample;

/**
 * User: xinxin
 * Date: 2016-10-26
 * Time: 15:20
 * FIXME
 */
//@Service(value = "ContractModelServiceImp")
@Service
public class ContractModelServiceImp implements ContractModelServiceI {
    @Autowired
    ContractModelMapper mContractModelMapper;

    @Override
    public ContractModel selectModelById(int id) {
        return mContractModelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ContractModel> selectModelByTypeId(int typeId) {
        ContractModelExample contractModelExample = new ContractModelExample();
        ContractModelExample.Criteria criteria = contractModelExample.createCriteria();
//        PageHelper.startPage(1, 10);
        criteria.andTypeIdEqualTo(typeId);
        contractModelExample.or(criteria);
        return mContractModelMapper.selectByExample(contractModelExample);
    }

    @Override
    public List<ContractModel> selectModelByTypeId(int typeId, int pageIndex, int perPage) {
        ContractModelExample contractModelExample = new ContractModelExample();
        ContractModelExample.Criteria criteria = contractModelExample.createCriteria();
        criteria.andTypeIdEqualTo(typeId);
        contractModelExample.or(criteria);
        PageHelper.startPage(pageIndex, perPage);
        return mContractModelMapper.selectByExample(contractModelExample);
    }

    @Override
    public List<ContractModel> selectModelByName(String name, int pageIndex, int perPage) {
        ContractModelExample contractModelExample = new ContractModelExample();
        ContractModelExample.Criteria criteria = contractModelExample.createCriteria();
        name = "%" + name + "%";
        criteria.andNameLike(name);
        contractModelExample.or(criteria);
        PageHelper.startPage(pageIndex, perPage);
        return mContractModelMapper.selectByExample(contractModelExample);
    }

    @Override
    public List<ContractModel> selectModelAll(int pageIndex, int perPage) {
        ContractModelExample contractModelExample = new ContractModelExample();
        ContractModelExample.Criteria criteria = contractModelExample.createCriteria();
        PageHelper.startPage(pageIndex, perPage);
        return mContractModelMapper.selectByExample(contractModelExample);
    }

    /**
     * 每页默认显示10条
     *
     * @param pageIndex
     * @return
     */
    @Override
    public List<ContractModel> selectModelAll(int pageIndex) {
        return selectModelAll(pageIndex, 10);
    }


    @Override
    public boolean newModel(ContractModel contractModel) {
        return mContractModelMapper.insert(contractModel) > 0;
    }

    @Override
    public boolean updateModel(ContractModel contractModel) {
        return mContractModelMapper.updateByPrimaryKey(contractModel) > 0;
    }

    @Override
    public boolean deleteModel(ContractModel contractModel) {
        return mContractModelMapper.deleteByPrimaryKey(contractModel.getId()) > 0;
    }

    @Override
    public boolean deleteModel(int contractId) {
        return mContractModelMapper.deleteByPrimaryKey(contractId) > 0;
    }
}
